package org.model.service.BigModelImpl;

import lombok.SneakyThrows;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.WebSocket;
import org.model.service.BigModel;
import org.model.service.modelListener;
import org.model.util.sendMsg;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static org.model.util.AuthUtil.getAuthUrl;

@Service
public class BigModelImpl implements BigModel {

//    public static ConcurrentHashMap<String,WebSocket> currentSocket=new ConcurrentHashMap<>();

    @SneakyThrows
    @Override
    public void talk(String message,String UUID) {

//        if (currentSocket.get(UUID) == null) {
            // 构建鉴权url
            String authUrl = getAuthUrl();
            OkHttpClient client = new OkHttpClient.Builder().build();
            String url = authUrl.replace("http://", "ws://").replace("https://", "wss://");
            Request request = new Request.Builder().url(url).build();
            WebSocket webSocket = client.newWebSocket(request, new modelListener(UUID));
//            currentSocket.put(UUID, webSocket);
//        }

//        WebSocket webSocket = currentSocket.get(UUID);
//        System.out.println(webSocket);
        sendMsg sendMsg = new sendMsg(webSocket, UUID);
        sendMsg.sendToModel(message);



    }
}
